package com.atguigu.gmall.auth.controller;

import com.atguigu.gmall.auth.service.AuthService;
import org.bouncycastle.math.raw.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {

    @Autowired
    private AuthService authService;

    @PostMapping("login")
    public String loin(@RequestParam("url")String url,
                       @RequestParam("username")String username,
                       @RequestParam("password")String password,
                       HttpServletRequest request,
                       HttpServletResponse response){

        authService.login(url,username,password,request,response);
        return "redirect:"+url;

    }

    @GetMapping("toLogin.html")
    public String toLogin(@RequestParam("returnUrl")String url, Model model){
        model.addAttribute("returnUrl",url);
        return "login";

    }
}
